<?php

/**
 * 文件分类控制器
 */

namespace app\admin\controller;

use think\Request;
use app\common\model\AttachmentCate;

use app\common\validate\AttachmentCateValidate;

class AttachmentCateController extends Controller
{

    //列表
    public function index(Request $request, AttachmentCate $model)
    {
        $param = $request->param();
        $model  = $model->scope('where', $param);

        $data = $model->paginate(['list_rows' => $this->admin['per_page'], 'query' => $request->get()]);
        //关键词，排序等赋值
        $this->assign($request->get());

        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),

        ]);
        return $this->fetch();
    }

    //添加
    public function add(Request $request, AttachmentCate $model, AttachmentCateValidate $validate)
    {
        if ($request->isPost()) {
            $param           = $request->param();
            $validate_result = $validate->scene('add')->check($param);
            if (!$validate_result) {
                return error($validate->getError());
            }
            $level = $model->where(['id'=>$param['parent_id']])->value('level');
            $param['level'] = $level +1;
            $result = $model::create($param);

            $url = URL_CLOSE_REFRESH;
            if (isset($param['_create']) && $param['_create'] == 1) {
                $url = URL_RELOAD;
            }
            return $result ? success('添加成功', $url) : error();
        }
        $cate = $model->column('id,parent_id,name', 'id');

        $this->initTree($cate);
        $parent_id = $request->param('parent_id') ?? 0;
        $selectData = $this->getMenuSelect(0, $parent_id);

        if ($parent_id == 0) {
            array_unshift($selectData, ['name' => '/', 'value' => 0, 'selected' => true]);
        } else {
            array_unshift($selectData, ['name' => '/', 'value' => 0]);
        }
        $select   = json_encode($selectData);
        $this->assign([
            'select'    => $select,
        ]);

        return $this->fetch();
    }

    //修改
    public function edit($id, Request $request, AttachmentCate $model, AttachmentCateValidate $validate)
    {

        $data = $model::find($id);
        if ($request->isPost()) {
            $param           = $request->param();
            $validate_result = $validate->scene('edit')->check($param);
            if (!$validate_result) {
                return error($validate->getError());
            }
            $level = $model->where(['id'=>$param['parent_id']])->value('level');
            $param['level'] = $level +1;
            $result = $data->save($param);
            return $result ? success('修改成功', URL_CLOSE_REFRESH)  : error();
        }
       
        return $this->fetch('add');
    }

    //删除
    public function del($id, AttachmentCate $model)
    {
        if (count($model->noDeletionId) > 0) {
            if (is_array($id)) {
                if (array_intersect($model->noDeletionId, $id)) {
                    return error('ID为' . implode(',', $model->noDeletionId) . '的数据无法删除');
                }
            } else if (in_array($id, $model->noDeletionId)) {
                return error('ID为' . $id . '的数据无法删除');
            }
        }

        if ($model->softDelete) {
            $result = $model->whereIn('id', $id)->useSoftDelete('delete_time', time())->delete();
        } else {
            $result = $model->whereIn('id', $id)->delete();
        }

        return $result ? success('操作成功', URL_RELOAD) : error();
    }

    protected function getMenuSelect($pid, $sid)
    {
        $child = $this->getChild($pid);
        $menu = [];
        foreach ($child as $key => $vo) {
            $child = $this->getChild($key);
            if ($key == $sid) {
                $menu[] = ['name' => $vo['name'], 'value' => $vo['id'], 'children' => $this->getMenuSelect($key, $sid), 'selected' => true];
            } else {
                $menu[] = ['name' => $vo['name'], 'value' => $vo['id'], 'children' => $this->getMenuSelect($key, $sid)];
            }
        }
        return $menu;
    }
}
